package com.xjc.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjc.business.entity.domain.*;
import com.xjc.business.entity.vo.coladmin.CheckBedReportVo;
import com.xjc.business.entity.vo.xlsx.ExportCheckBedTableVo;
import com.xjc.business.service.*;
import com.xjc.business.mapper.CheckBedBackMapper;
import com.xjc.system.constants.MyConstants;
import com.xjc.system.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author root
* @description 针对表【check_bed_back(查寝反馈)】的数据库操作Service实现
* @createDate 2025-03-09 09:46:55
*/
@Service
public class CheckBedBackServiceImpl extends ServiceImpl<CheckBedBackMapper, CheckBedBack>
    implements CheckBedBackService{

    @Autowired
    @Lazy
    CollageAdminRelationsService collageAdminRelationsService;

    @Autowired
    @Lazy
    CheckBedBackStudentService checkBedBackStudentService;

    @Autowired
    @Lazy
    MajorsService majorsService;

    @Autowired
    @Lazy
    ClazzsService clazzsService;

    @Autowired
    @Lazy
    StudentsService studentsService;

    @Autowired
    @Lazy
    AllStudentStayRelationsService allStudentStayRelationsService;

    @Autowired
    @Lazy
    BuildingsService buildingsService;

    @Autowired
    @Lazy
    DormitoriesService dormitoriesService;

    @Autowired
    @Lazy
    BedsService bedsService;

    @Autowired
    @Lazy
    CollegesService colageService;

    /**
     * 获取宿舍检查报告列表
     * @param id 学院管理员id
     * @return
     */
    @Override
    public Result<List<CheckBedReportVo>> getCheckBedReportList(Integer id) {

        LambdaQueryWrapper<CollageAdminRelations> collageAdminRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        collageAdminRelationsLambdaQueryWrapper.eq(CollageAdminRelations::getColAdminId,id);
        CollageAdminRelations collageAdminRelations = collageAdminRelationsService.getOne(collageAdminRelationsLambdaQueryWrapper);
        Integer colId = collageAdminRelations.getColId();

        LambdaQueryWrapper<CheckBedBack> checkBedBackLambdaQueryWrapper = new LambdaQueryWrapper<>();
        checkBedBackLambdaQueryWrapper.eq(CheckBedBack::getCollegeId,colId);

        List<CheckBedReportVo> checkBedReportVoList = list(checkBedBackLambdaQueryWrapper).stream().map(checkBedBack -> {
            CheckBedReportVo checkBedReportVo = new CheckBedReportVo();
            checkBedReportVo.setId(checkBedBack.getId());
            checkBedReportVo.setTypeOne(MyConstants.CHECK_BED_TYPE_ONE.get(checkBedBack.getTypeOne()));
            checkBedReportVo.setTypeTwo(MyConstants.CHECK_BED_TYPE_TWO.get(checkBedBack.getTypeTwo()));
            checkBedReportVo.setStartTime(checkBedBack.getStartTime());

            LambdaQueryWrapper<CheckBedBackStudent> checkBedBackStudentLambdaQueryWrapper = new LambdaQueryWrapper<>();
            checkBedBackStudentLambdaQueryWrapper.eq(CheckBedBackStudent::getBackId,checkBedBack.getId());

            List<CheckBedReportVo.CheckBedReportStudentVo> checkBedReportStudentVoList = checkBedBackStudentService.list(checkBedBackStudentLambdaQueryWrapper).stream().map(checkBedBackStudent -> {
                CheckBedReportVo.CheckBedReportStudentVo checkBedReportStudentVo = new CheckBedReportVo.CheckBedReportStudentVo();

                Integer majId = checkBedBackStudent.getMajId();
                Integer classId = checkBedBackStudent.getClassId();
                Integer stuId = checkBedBackStudent.getStuId();

                checkBedReportStudentVo.setStuInfo(majorsService.getById(majId).getName() + "专业 " + clazzsService.getById(classId).getNum() + "班 " + studentsService.getById(stuId).getName());

                LambdaQueryWrapper<AllStudentStayRelations> allStudentStayRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
                allStudentStayRelationsLambdaQueryWrapper.eq(AllStudentStayRelations::getStuId,stuId);
                AllStudentStayRelations allStudentStayRelations = allStudentStayRelationsService.getOne(allStudentStayRelationsLambdaQueryWrapper);
                checkBedReportStudentVo.setBedInfo(buildingsService.getById(allStudentStayRelations.getBuildId()).getBno() + "号楼 " + dormitoriesService.getById(allStudentStayRelations.getDormId()).getDno() + "宿舍 " + bedsService.getById(allStudentStayRelations.getBedId()).getBno() + "床");

                return checkBedReportStudentVo;
            }).collect(Collectors.toList());

            checkBedReportVo.setCheckBedStudentVoList(checkBedReportStudentVoList);
            checkBedReportVo.setNum(checkBedReportStudentVoList.size());

            return checkBedReportVo;
        }).collect(Collectors.toList());

        return Result.okWithData(checkBedReportVoList);
    }

    /**
     * 获取宿舍检查表格数据
     * @param checkBedBackId 宿舍检查报告id
     */
    @Override
    public ExportCheckBedTableVo getExportCheckBedTableVoList(Integer checkBedBackId) {

        ExportCheckBedTableVo exportCheckBedTableVo = new ExportCheckBedTableVo();
        CheckBedBack checkBedBack = getById(checkBedBackId);
        Integer collegeId = checkBedBack.getCollegeId();
        exportCheckBedTableVo.setCollegeName(colageService.getById(collegeId).getName());
        exportCheckBedTableVo.setTypeOne(MyConstants.CHECK_BED_TYPE_ONE.get(checkBedBack.getTypeOne()));
        exportCheckBedTableVo.setTypeTwo(MyConstants.CHECK_BED_TYPE_TWO.get(checkBedBack.getTypeTwo()));
        Date startTime = checkBedBack.getStartTime();
        exportCheckBedTableVo.setTime(1900 + startTime.getYear()+"年"+(startTime.getMonth()+1)+"月"+startTime.getDate()+"日");

        LambdaQueryWrapper<CheckBedBackStudent> checkBedBackStudentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        checkBedBackStudentLambdaQueryWrapper.eq(CheckBedBackStudent::getBackId,checkBedBackId);

        List<ExportCheckBedTableVo.StuInfoVo> stuInfoVoList = checkBedBackStudentService.list(checkBedBackStudentLambdaQueryWrapper).stream().map(checkBedBackStudent -> {
            ExportCheckBedTableVo.StuInfoVo stuInfoVo = new ExportCheckBedTableVo.StuInfoVo();
            Integer stuId = checkBedBackStudent.getStuId();
            Students students = studentsService.getById(stuId);
            stuInfoVo.setSno(students.getSno());
            stuInfoVo.setStuName(students.getName());
            stuInfoVo.setSex(students.getSex() == 1 ? "男" : "女");
            stuInfoVo.setMajorClass(majorsService.getById(checkBedBackStudent.getMajId()).getName() + "专业 " + clazzsService.getById(checkBedBackStudent.getClassId()).getNum() + "班");
            LambdaQueryWrapper<AllStudentStayRelations> allStudentStayRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            allStudentStayRelationsLambdaQueryWrapper.eq(AllStudentStayRelations::getStuId,stuId);
            Integer dormId = allStudentStayRelationsService.getOne(allStudentStayRelationsLambdaQueryWrapper).getDormId();
            stuInfoVo.setBedNum(dormitoriesService.getById(dormId).getDno() + "");

            return stuInfoVo;
        }).collect(Collectors.toList());

        exportCheckBedTableVo.setStuInfoVoList(stuInfoVoList);

        return exportCheckBedTableVo;
    }
}




